package com.yc.AdminUI;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.UI.WelcomeUI;
import com.yc.dao.AdminDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;
import com.yc.utils.Values;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 *管理员登录界面
 * @author UNO
 * 2021年6月21日下午3:33:44
 */
public class AdminLoginUI {

	protected Shell shell;
	private boolean down = false;
	private int clickX = 0;
	private int clickY = 0;
	private Text text_pwd;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AdminLoginUI window = new AdminLoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NONE);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(905, 507);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		//背景图片
		composite.setBackgroundImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/back.jpg"));
		// 设置窗口居中
		SWTUtil.centerWindom(shell);


		//设置图标
		Label label_Icon = new Label(composite, SWT.NONE);
		label_Icon.setBounds(0, 0, 40, 40);
		label_Icon.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/福字.png"));

		//关闭按钮
		Label label_close = new Label(composite, SWT.NONE);

		label_close.setBounds(863, 0, 40, 40);
		label_close.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/关闭.png"));

		//最小化按钮
		Label label_min = new Label(composite, SWT.NONE);
		label_min.setBounds(817, 0, 40, 40);
		label_min.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/最小化.png"));

		Label Label_account = new Label(composite, SWT.CENTER);
		Label_account.setText("姓名");
		Label_account.setFont(SWTResourceManager.getFont("Papyrus", 14, SWT.NORMAL));
		Label_account.setBounds(228, 151, 92, 40);

		text_pwd = new Text(composite, SWT.BORDER);
		text_pwd.setText("a");
		text_pwd.setBounds(486, 251, 119, 26);

		Label label_pwd = new Label(composite, SWT.CENTER);
		label_pwd.setText("密码");
		label_pwd.setFont(SWTResourceManager.getFont("Papyrus", 14, SWT.NORMAL));
		label_pwd.setBounds(244, 245, 60, 40);

		Label Label_title = new Label(composite, SWT.CENTER);
		Label_title.setText("管理员登录");
		Label_title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		Label_title.setBounds(46, 10, 97, 30);

		Button Button_login = new Button(composite, SWT.CENTER);
		Button_login.setText("登录");
		Button_login.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		Button_login.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		Button_login.setBounds(351, 408, 119, 40);

		Combo combo_account = new Combo(composite, SWT.NONE);
		combo_account.setItems(new String[] {"QJH"});
		combo_account.setBounds(486, 157, 92, 28);
		combo_account.setText("UNO");

		Label Label_return = new Label(composite, SWT.CENTER);
		Label_return.setImage(SWTResourceManager.getImage(AdminLoginUI.class, "/images/return.png"));
		Label_return.setBounds(10, 46, 40, 40);

		//关闭按钮操作
		label_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.dispose();
				System.exit(0);
			}
		});
		//最小化操作
		label_min.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		//面板拖动
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				down = true;
				clickX = e.x;
				clickY = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				down = false;
			}
		});

		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(down) {  //down为true，说明鼠标按下，窗口跟着鼠标移动			
					shell.setLocation(shell.getLocation().x + e.x - clickX, shell.getLocation().y + e.y - clickY);
				}

			}
		});
		//返回
		Label_return.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				WelcomeUI wc = new WelcomeUI();
				shell.dispose();
				wc.open();
			}
		});
		//点击进行管理员登录
		Button_login.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				String account = combo_account.getText().trim();
				String pwd = text_pwd.getText().trim();
				
				//判空
				if(StringUtil.checkNull(account)) {
					combo_account.setFocus();  //聚焦
					return;
				}
				
				AdminDao admindao = new AdminDao();
				Map<String, Object> map = admindao.login(account, pwd);
				
				if (map == null || map.isEmpty()) { //说明没有取到值，登录失败
					SWTUtil.showMessage(shell, "错误提示", "管理员登录失败...");
					return;
				}
				
				//如果取到值，说明登录成功  map里存的是账号和密码
				Values.currentLoginUser = map;
				AdminMainUI m = new AdminMainUI();
				shell.dispose();
				m.open();
			}
		});
	}
}
